﻿using System;
using Microsoft.FxCop.Sdk;

namespace Community.StaticAnalysis.Design.Rules
{
    /// <summary>
    /// The <see cref="MarkDataContractPropertiesWithDataMember"/>
    /// class is used to identify classes marked with the DataContract attribute which define properties
    /// that are not marked with the DataMember attribute.
    /// </summary>
    internal sealed class MarkDataContractPropertiesWithDataMember : CheckPropertyRule
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="MarkDataContractPropertiesWithDataMember"/> class.
        /// </summary>
        public MarkDataContractPropertiesWithDataMember()
            : base(
                typeof (MarkDataContractPropertiesWithDataMember).Name,
                typeof(MarkDataContractPropertiesWithDataMember).Namespace + Constants.RulesResourceNameSuffix,
                typeof (MarkDataContractPropertiesWithDataMember).Assembly)
        {
        }

        /// <summary>
        /// Checks the specified property.
        /// </summary>
        /// <param name="property">The property.</param>
        public override void Check(PropertyNode property)
        {
            // Check if the class is marked with DataContract
            if (RulesHelper.NodeHasAttribute(property.DeclaringType, Constants.DataContractFullName) == false)
            {
                // This class is not a data contract
                return;
            }

            // Check if this property is marked with DataMember
            if (RulesHelper.NodeHasAttribute(property, Constants.DataMemberFullName) == false)
            {
                // This is a data contract class but this property isn't marked with DataMember
                Problems.Add(new Problem(GetResolution(new Object[] { property.FullName })));
            }
        }
    }
}